package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HatchetSystemUser;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.HatchetSystemUserMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IHatchetSystemUserService;

/**
 * 卡时代用户 服务层实现
 * 
 * @author ruoyi
 * @date 2019-02-16
 */
@Service
public class HatchetSystemUserServiceImpl implements IHatchetSystemUserService 
{
	@Autowired
	private HatchetSystemUserMapper hatchetSystemUserMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
     * 查询卡时代用户信息
     * 
     * @param iD 卡时代用户ID
     * @return 卡时代用户信息
     */
    @Override
	public HatchetSystemUser selectHatchetSystemUserById(String iD)
	{
	    return hatchetSystemUserMapper.selectHatchetSystemUserById(iD);
	}
	
	/**
     * 查询卡时代用户列表
     * 
     * @param hatchetSystemUser 卡时代用户信息
     * @return 卡时代用户集合
     */
	@Override
	public List<HatchetSystemUser> selectHatchetSystemUserList(HatchetSystemUser hatchetSystemUser)
	{
	    return hatchetSystemUserMapper.selectHatchetSystemUserList(hatchetSystemUser);
	}
	
    /**
     * 新增卡时代用户
     * 
     * @param hatchetSystemUser 卡时代用户信息
     * @return 结果
     */
	@Override
	public int insertHatchetSystemUser(HatchetSystemUser hatchetSystemUser)
	{
	    return hatchetSystemUserMapper.insertHatchetSystemUser(hatchetSystemUser);
	}
	
	/**
     * 修改卡时代用户
     * 
     * @param hatchetSystemUser 卡时代用户信息
     * @return 结果
     */
	@Override
	public int updateHatchetSystemUser(HatchetSystemUser hatchetSystemUser)
	{
	    return hatchetSystemUserMapper.updateHatchetSystemUser(hatchetSystemUser);
	}

	/**
     * 删除卡时代用户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteHatchetSystemUserByIds(String ids)
	{
		return hatchetSystemUserMapper.deleteHatchetSystemUserByIds(Convert.toStrArray(ids));
	}

	@Override
	public HatchetSystemUser selectSystemUserByAgentNo(String agentNo) {
		return hatchetSystemUserMapper.selectSystemUserByAgentNo(agentNo);
	}
	
	/**
	 * 同步数据库密码
	 */
	public void updatePassWord() {
	    	SysUser user = new SysUser();
	    	//查询所有的用户
	    	List<SysUser> userList =sysUserMapper.selectUserList(user);
	    	StringBuffer stringBuffer = new StringBuffer();
	    	 /*HatchetSystemUser hatchetSystemUser = new HatchetSystemUser();
	      	List<HatchetSystemUser> systemUserList = selectHatchetSystemUserList(hatchetSystemUser);
	  		for (int i = 0; i < systemUserList.size(); i++) {
	    		boolean flag = false;
				for (int j = 0; j < userList.size(); j++) {
					if (systemUserList.get(i).getLoginName().equals(userList.get(j).getLoginName())) {
						flag = true;
					}
				}
				if (!flag) {
					stringBuffer.append(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+systemUserList.get(i).getLoginName()+"用户名称"+systemUserList.get(i).getRealName());
				}
			}*/	
	    	int count = 0;
	    	if (StringUtils.isNotEmpty(userList)) {
	    		for (int i = 0; i < userList.size(); i++) {
	    			try {
	    				//查询用户的
		    			HatchetSystemUser systemUser = selectSystemUserByAgentNo(userList.get(i).getLoginName());
		    			if (StringUtils.isNotNull(systemUser)) {
		    				userList.get(i).setPassword(systemUser.getPASSWORD());
			    			int updateUser2 = sysUserMapper.updateUser(userList.get(i));
			    			if (updateUser2<1) {
			    				System.out.println(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
			    				stringBuffer.append(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
			    			}else {
								count ++;
							}
		    			}else {
		    				System.out.println(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
		    				stringBuffer.append(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
		    			}
					} catch (Exception e) {
						System.out.println(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
						stringBuffer.append(">>>>>>>>>>>>>>>>>>>>修改失败的用户"+userList.get(i).getLoginName()+"用户名称"+userList.get(i).getUserName());
					}
	    		}
			}
	    	System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>修改成功的用户数量"+count);
	    	System.out.println(stringBuffer);
	    }

	
}
